#!/usr/bin/perl

# Inspired from Mageia script:
# https://gitweb.mageia.org/software/rpm/rpm-setup/
# Licensed under GPL-2.0-or-later (maybe?)
#   via: https://svnweb.mageia.org/packages/cauldron/rpm-mageia-setup/current/SPECS/rpm-mageia-setup.spec
#
# Written by Funda Wang <fundawang@yeah.net>, Nov 2025
#
# Current supported options:
#   --requires
#   --buildrequires
#   --recommends
#   --test
#       Output corresponding fields in META.json/yml. Maybe use in combination.
#       Default output nothing.

use strict;
use warnings;

use JSON qw{ from_json };
use YAML qw{ Load      };
use Getopt::Long;

my $process_requires = 0;
my $process_buildrequires = 0;
my $process_test = 0;
my $process_recommends = 0;

GetOptions ('requires!' => \$process_requires, 'buildrequires!' => \$process_buildrequires, 'recommends!' => \$process_recommends, 'test!' => \$process_test);

# slurp the file
my $path = <>;
chomp($path);

my $fh;
open $fh, '<', $path 
  or die "can't open $path: $!";
my $data = do { local $/; <$fh> };
close $fh;

# parse meta - either yaml or json
my $meta = $path =~ /\.yml$/
    ? Load( $data )
    : from_json( $data );

my $module;
my $requires;

if ($process_requires) {
  # dump the runtime requires with their version
  $requires = $meta->{"meta-spec"}{version} >= 2
      ? $meta->{prereqs}{runtime}{requires}
      : $meta->{requires};
  foreach $module ( sort keys %$requires ) {
      next if $module eq 'perl'; # minimum perl version
      my $version = $requires->{$module};
      if ( $version == 0 ) {
          print "perl($module)\n";
      } else {
          print "perl($module) >= $version\n";
      }
  }
}

if ($process_buildrequires) {
  # dump the build requires with their version
  $requires = $meta->{"meta-spec"}{version} >= 2
      ? $meta->{prereqs}{build}{requires}
      : $meta->{build_requires};
  foreach $module ( sort keys %$requires ) {
      next if $module eq 'perl'; # minimum perl version
      my $version = $requires->{$module};
      if ( $version == 0 ) {
          print "perl($module)\n";
      } else {
          print "perl($module) >= $version\n";
      }
  }

  # dump the configure requires with their version
  $requires = $meta->{"meta-spec"}{version} >= 2
      ? $meta->{prereqs}{configure}{requires}
      : $meta->{configure_requires};
  foreach $module ( sort keys %$requires ) {
      next if $module eq 'perl'; # minimum perl version
      my $version = $requires->{$module};
      if ( $version == 0 ) {
          print "perl($module)\n";
      } else {
          print "perl($module) >= $version\n";
      }
  }
}

if ($process_recommends) {
  # dump the recommends with their version
  my $recommends = $meta->{"meta-spec"}{version} >= 2
      ? $meta->{prereqs}{runtime}{recommends}
      : $meta->{recommends};
  foreach my $module ( sort keys %$recommends ) {
      next if $module eq 'perl'; # minimum perl version
      my $version = $recommends->{$module};
      if ( $version == 0 ) {
          print "perl($module)\n";
      } else {
          print "perl($module) >= $version\n";
      }
  }
}

if ($meta->{"meta-spec"}{version} >= 2) {
  if($process_test) {
    # dump the test requires with their version
    $requires = $meta->{prereqs}{test}{requires};
    foreach $module ( sort keys %$requires ) {
       next if $module eq 'perl'; # minimum perl version
       my $version = $requires->{$module};
       if ( $version == 0 ) {
           print "perl($module)\n";
       } else {
           print "perl($module) >= $version\n";
       }
    }
  }
}

exit;
